import axios from 'axios'
import qs from 'qs'
import global from '../global.js'

if (process.env.NODE_ENV == 'development') {
  axios.defaults.baseURL = '/wf'
}

axios.defaults.withCredentials = false
axios.defaults.headers = {
  'X-Requested-With': 'XMLHttpRequest',
}
axios.defaults.timeout = 1000 * 60 * 5

// 添加请求拦截
axios.interceptors.request.use(req => {
  if (global.username) {
    req.headers.username = global.username;
  }
  return req;
}, error => {
  return Promise.reject(error);
})

// 添加响应拦截器
axios.interceptors.response.use(resp => {
  var res = resp.data || {}
  if (!res.success) {
    console.log('failure', arguments)
  }
  return res;
}, error => {
  return Promise.reject(error);
});

function submitForm(url, data) {
  return axios.post(url, qs.stringify(data), {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export default {
  listFlow: function() {
    return axios.get('/flow/list')
  },
  listAssignable: function(startPage, limit) {
    return axios.get('/flow/assignable', {
      params: {
        startPage,
        limit
      }
    })
  },
  listMytask: function(startPage, limit) {
    return axios.get('/flow/mytask', {
      params: {
        startPage,
        limit
      }
    })
  },
  listRunning: function(startPage, limit) {
    return axios.get('/flow/running', {
      params: {
        startPage,
        limit
      }
    })
  },
  listSuspended: function(startPage, limit) {
    return axios.get('/flow/suspended', {
      params: {
        startPage,
        limit
      }
    })
  },
  listFinished: function(startPage, limit) {
    return axios.get('/flow/finished', {
      params: {
        startPage,
        limit
      }
    })
  },
  listCandidate: function(taskId) {
    return axios.get('/flow/candidate/' + taskId)
  },
  processHistory: function(processInstanceId) {
    return axios.get('/flow/history/' + processInstanceId)
  },
  processVariables: function(processInstanceId) {
    return axios.get('/flow/processVariables/' + processInstanceId)
  },
  taskVariables: function(activityInstanceId) {
    return axios.get('/flow/taskVariables/' + activityInstanceId)
  },
  startFlow: function(flowKey, variables) {
    return axios.post('/flow/start/' + flowKey, variables)
  },
  terminateFlow: function(instanceId, reason) {
    return submitForm('/flow/terminate/' + instanceId, {
      reason
    })
  },
  pauseFlow: function(instanceId, reason) {
    return submitForm('/flow/pause/' + instanceId)
  },
  resumeFlow: function(instanceId, reason) {
    return submitForm('/flow/resume/' + instanceId)
  },
  completeTask: function(instanceId, variables, comment) {
    return axios.post('/flow/complete/' + instanceId, {
      variables,
      comment
    })
  },
  claimTask: function(taskId) {
    return axios.post('/flow/claim/' + taskId)
  },
  assignTask: function(taskId, transactor) {
    return submitForm('/flow/assign/' + taskId, {
      transactor
    })
  },
  delegateTask: function(taskId, target) {
    return submitForm('/flow/delegate/' + taskId, {
      target
    })
  },
  backTask: function(taskId, comment) {
    return submitForm('/flow/back/' + taskId, {
      comment
    })
  },
}
